package com.sword.controller.stock;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.stock.Stockpile;
import com.sword.pojo.stock.Storehouse;
import com.sword.service.impl.stock.StockhouseServiceImpl;
import com.sword.service.stock.StockhouseService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StockhouseController {
    @Autowired
    private StockhouseService stockhouseService;

    @RequestMapping("/stock/stockhouse")
    public String stockhouse() {
        return "views/stock/stockhouse";
    }

    /*
    查询所有仓库
     */
    @ResponseBody
    @RequestMapping("/stock/queryStorehouse")
    public PageResult queryStorehouse(@RequestBody QueryPageBean queryPageBean) {
        try {
            return stockhouseService.queryStorehouse(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_STOREHOUSE_FAIL, null, null);
        }
    }
    /*
    添加仓库
     */
    @ResponseBody
    @RequestMapping("/stock/addStorehouse")
    public Result addStorehouse(@RequestBody Storehouse storehouse) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            storehouse.setEmployeeid(userId);

            // 判断是否添加成功
            Integer flag = stockhouseService.addStorehouse(storehouse);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_PRODOCTSPEC_FAIL, null);
            }

        } catch (Exception e) {
            return new Result(500, MessageConstant.ADD_PRODOCTSPEC_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_PRODOCTSPEC_SUCCESS, null);
    }
    /*
    修改仓库信息
     */
    @ResponseBody
    @RequestMapping("/stock/editStorehouse")
    public Result editStorehouse(@RequestBody Storehouse storehouse) {
        try {
            Integer flag = stockhouseService.editStorehouse(storehouse);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_STOREHOUSE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_STOREHOUSE_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_STOREHOUSE_SUCCESS, null);
    }
    /**
     * 删除仓库
     */

    @ResponseBody
    @RequestMapping("/stock/deleteStorehouse")
    public Result deleteStorehouse(Integer id) {
        try {
            Integer flag = stockhouseService.deleteStorehouse(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_STOREHOUSE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_STOREHOUSE_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_STOREHOUSE_SUCCESS, null);
    }
}
